<template>
  <div>
    <ul class="list">
      <li v-for="(item, index) in listData" :key="index" >
        <div class="title">
          <span class="classify">【<i class="iconfont icon-rencai"></i>人才】</span>
          <h5 @click="toDetail(item)"><span v-html="item['name_wordsLight']"></span><i class="line"></i><span v-html="item['org_wordsLight']"></span></h5>
        </div>
        <div class="patent-list">
          <p v-for="(citem, index2) in item.patentList" :key="index2">
            <i class="icon"></i><span v-html="citem['pn_wordsLight']"></span><span v-html="citem['title_wordsLight']"></span>
          </p>
          <p v-if="item.patentListLength > 3">...</p>
        </div>
        <!-- 收藏 -->
        <!-- <div class="collect-btn" @click="collet(item, index)">
          <span class="iconfont"
            :class="{'active': item.isCollect, 'icon-shoucang-': item.isCollect, 'icon-shoucang': !item.isCollect}"
          ></span>
          <span v-if="item.isCollect" class="label">已收藏</span>
        </div> -->
      </li>
      <!-- no-data -->
      <div v-if="noData" class="no-data">
        <loading1 :emptyText="emptyText"/>
      </div>
    </ul>
    <el-pagination
      v-if="listAllData.length>0"
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      :current-page.sync="opts['ipi']"
      :page-size="opts['ips']"
      :total="total"
      class="elpagination"
    ></el-pagination>
  </div>
</template>

<script>
import {thousands, wordsLight} from '@/utils/utils'
import qs from 'querystring'
export default {
  data() {
    return {
      opts: {},
      sort: 1,
      listData: [],
      listAllData: [],
      total: 0,
      noData: false,
      emptyText: '',
      facetParams: {}, // 聚合过来的参数
    }
  },
  methods: {
    init(opts) {
      this.opts = JSON.parse(JSON.stringify(opts))
      // 
      if (this.opts.resetFacet) {
        this.facetParams = {}
      }
      this.search()
    },
    search() {
      this.listData = []
      this.listAllData = []
      this.noData = true
      this.emptyText = 'loading'
      let params = {
        ipi: this.opts.ipi,
        ips: this.opts.ips,
        // order: this.opts.sort,
        region: '',
        companyName: ''
      }
      if (this.opts.sort !== -1) {
        params['order'] = this.opts.sort
      }
      // 聚合的参数
      if (Object.values(this.facetParams).length > 0) {
        params['filter'] = JSON.stringify(this.facetParams)
      }
      // 判断是否有高级检索
      if (this.opts.advanced) {
        if (this.opts.accurateSearch !== '') {
          params['accurateSearch'] = this.opts.accurateSearch
        }
        if (this.opts.includeSearch !== '') {
          params['includeSearch'] = this.opts.includeSearch
        }
        if (this.opts.notIncludeSearch !== '') {
          params['notIncludeSearch'] = this.opts.notIncludeSearch
        }
      } else {
        if (this.opts.searchTxt !== '') {
          params['scope'] = this.opts.searchTxt
        }
      }
      this.$get(this.$Url.EnergyTalent.listPage, params).then((res) => {
        let data = res.data
        let result = data.result || []
        if (result.length <= 0) {
          this.emptyText = '无法找到你所查询的内容，换个关键词试试'
        } else {
          this.noData = false
        }
        // 配置高亮
        let settings = []
        let searchTxtArr = this.opts.searchTxt.split('')
        searchTxtArr.forEach(v => {
          settings.push({
            words: v,
            color: '#FF8C00'
          })
        })
        result.forEach(v => {
          v.name_wordsLight = wordsLight(v.name, settings)
          v.org_wordsLight = wordsLight(v.org, settings)
        })
        this.listAllData = JSON.parse(JSON.stringify(result))
        this.listAllData.forEach((v, index) => {
          let valIndex = (this.opts.ipi - 1) * this.opts.ips
          if (index >= valIndex && index < (valIndex + this.opts.ips)) {
            this.getPatent(v.pid, this.listAllData[index])
            if (v.isCollect === undefined) {
              // this.isCollect(v.pid, this.listAllData[index])
              // this.getPatent(v.pid, this.listAllData[index])
            }
          }
        })
        this.listData = this.listAllData.filter((v, index) => {
          let valIndex = (this.opts.ipi - 1) * this.opts.ips
          return index >= valIndex && index < (valIndex + this.opts.ips)
        })
        let _total = data.total || 0
        this.total = _total
        this.$emit('resultTotal', thousands(_total))
        // 聚合
        if (this.opts.resetFacet) {
          let facetData = {
            org: data.org, // 所属机构
            patentCount: data.patentCount // 专利数量
          }
          this.$Bus.$emit('facetData', facetData)
        }
      }).catch(err => {
        this.emptyText = '无法找到你所查询的内容，换个关键词试试'
      })
    },
    // 获取专利
    getPatent(pid, sth) {
      // 配置高亮
      let settings = []
      let searchTxtArr = this.opts.searchTxt.split('')
      searchTxtArr.forEach(v => {
        settings.push({
          words: v,
          color: '#FF8C00'
        })
      })
      this.$get(this.$Url.EnergyTalent.patentListByExpert, {
        ipi: 1,
        ips: 3,
        pid,
        keyword: this.opts.searchTxt
      }).then(res => {
        let result = res.data && res.data.result
        result.forEach(v => {
          v.pn_wordsLight = wordsLight(v.pn, settings)
          v.title_wordsLight = wordsLight(v.title, settings)
        })
        this.$set(sth, "patentList", result);
        this.$set(sth, "patentListLength", res.data.total || 0);
      }).catch(err => {})
    },
    // 是否收藏
    isCollect(pid, sth) {
      this.$get(this.$Url.EnergyTalent.isCollect, {
        pid
      }).then(res => {
        let data = res.data
        this.$set(sth, "isCollect", data);
      }).catch(err => {})
    },
    collet(item, index) {
      if (item.isCollect) {
        // 取消收藏
        this.$get(this.$Url.EnergyTalent.cancelCollectExpert, {
          pid: item.pid
        }).then(res => {
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: "取消收藏成功"
            });
            this.$set(this.listData[index], "isCollect", !item.isCollect);
          } else {
            this.$message({
              type: "error",
              message: "取消收藏失败",
              duration: 1000
            });
          }
        }).catch(e => {console.log(e);});
      } else {
        this.$get(this.$Url.EnergyTalent.collectExpert, {
          pid: item.pid
        }).then(res => {
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: "收藏成功",
              duration: 1000
            });
            this.$set(this.listData[index], "isCollect", !item.isCollect);
          } else {
            this.$message({
              type: "error",
              message: "收藏失败",
              duration: 1000
            });
          }
        }).catch(e => {console.log(e);});
      }
    },
    // 分页
    handleCurrentChange(val) {
      this.opts.ipi = val
      this.listAllData.forEach((v, index) => {
        let valIndex = (this.opts.ipi - 1) * this.opts.ips
        if (index >= valIndex && index < (valIndex + this.opts.ips)) {
          this.getPatent(v.pid, this.listAllData[index])
          if (v.isCollect === undefined) {
            // this.isCollect(v.pid, this.listAllData[index])
            // this.getPatent(v.pid, this.listAllData[index])
          }
        }
      })
      
      this.listData = this.listAllData.filter((v, index) => {
        let valIndex = (this.opts.ipi - 1) * this.opts.ips
        return index >= valIndex && index < (valIndex + this.opts.ips)
      })
    },
    // 进入详情页
    toDetail(item){
      this.$router.push({
        name: 'EnergyTalentDetailIntroduction',
        query: {
          key: item.pid || '',
          name: item.name || '',
          companyName: item.org || '',
          count: item.count || 0
        }
      })
    },
  },
  mounted() {
    this.$Bus.$on('facetParams', params => {
      this.facetParams = params
      this.$set(this.opts, 'ipi', 1)
      this.search()
    })
  },
  beforeDestroy() {
    this.$Bus.$off('facetParams')
  }
}
</script>

<style lang="scss" scoped>
.list {
  position: relative;
  min-height: 1.3rem;
  li {
    position: relative;
    padding-bottom: .14rem;
    border-bottom: 1px solid $color36;
    .title {
      position: relative;
      line-height: .48rem;
      padding-left: 0;
      padding-right: .6rem;
      border-bottom: 0;
      .classify {
        display: inline-block;
        min-width: .85rem;
        margin-right: .05rem;
        font-size: .14rem;
        vertical-align: middle;
        background-image: linear-gradient(50deg, $color55, $color56);
        background-clip:text;
        color: transparent;
        i {
          display: inline-block;
          padding: 0 .04rem;
        }
      }
      h5 {
        display: inline-block;
        width: calc(100% - 1.5rem);
        // font-family: SourceHanSansCN-Medium;
        font-weight: normal;
        font-size: .18rem;
        vertical-align: middle;
        color: $color57;
        cursor: pointer;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        ::v-deep {
          em {
            color: $color120;
          }
        }
        span {
          position: relative;
          &:nth-of-type(1) {
            &:before {
              position: absolute;
              top: 16%;
              right: calc(-.16rem);
              width: 2px;
              height: 74%;
              content: '';
              background-color: $color57;
            }
          }
          &:nth-of-type(2) {
            font-size: .15rem;
          }
        }
        .line {
          position: relative;
          display: inline-block;
          padding: 0 .16rem;
        }
      }
    }
    .patent-list {
      position: relative;
      padding-left: .05rem;
      p {
        line-height: .24rem;
        padding: .05rem 0;
        font-size: .15rem;
        color: $color58;
        &:nth-last-child(2) {
          padding-bottom: 0;
        }
        &:last-of-type {
          line-height: 1;
          padding: 0;
        }
        .icon {
          position: relative;
          display: inline-block;
          width: .12rem;
          height: .12rem;
          margin-right: .06rem;
          background: url(../../../../../assets/img/IR/list-icon.png) no-repeat center center;
          background-size: 100% 100%;
        }
        span {
          display: inline-block;
          margin-right: .1rem;
        }
      }
    }
    .collect-btn {
      position: absolute;
      top: .14rem;
      right: 0;
      display: block;
      font-size: .16rem;
      line-height: .2rem;
      text-align: center;
      color: $color120;
      cursor: pointer;
      span {
        display: inline-block;
        vertical-align: middle;
        &.iconfont {
          font-size: .24rem;
        }
        &.label {
          margin-left: .05rem;
          font-size: .16rem;
        }
      }
      &.score {
        background-color: #8696E8;
      }
      &.el-icon-star-off {
        font-size: 18px;
        background: #909399;
        cursor: pointer;
      }
      &.active {
        font-size: 21px;
        background-color: #8696E8;
      }
    }
  }
}
.no-data {
  line-height: 1.2rem;
}
.elpagination {
  padding: .2rem 0;
  ::v-deep {
    .el-pager {
      li {
        font-weight: normal;
        &.active {
          font-size: 16px;
          font-weight: bold;
          color: $mainColor;
        }
      }
    }
    .more + .number {
      display: none;
    }
  }
}
</style>